var form;
var layer;
//页面初始化时加载Layui框架
layui.use(['form', 'layer', 'laydate'], function () {
    form = layui.form();
    layer = layui.layer;

    //紧急程度判断
    var jobStateTemp = $("#jobStateTemp").val();
    if (jobStateTemp != null && jobStateTemp != "") {
        $("#jobState").val(jobStateTemp);
        form.render();
    }
    //关闭
    var closed = $(".close");
    closed.click(function() {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    });
    //监听提交
    form.on('submit(submit)', function(){
        //提交按钮置为不可用
        $("#submit").addClass("layui-btn-disabled");
        addOrUpdateJob();
    });
});


/**
 * 新增/修改
 */
function addOrUpdateJob() {
    var jobId = $.trim($("#jobId").val());
    // 紧急程度
    var jobState = $("#jobState").val();
    // 客户Id
    var customerId = $.trim($("#customerId").val());
    // 备注
    var remark = $("#remark").val();

    var type = $("#type").val();

    if (null == type || '' == type) {
        type = 'add';
    }

    var paramData = {
        "job.id" : jobId,
        "job.state" : jobState,
        "job.customerId" : customerId,
        "job.remark" : remark,
        "type" : type
    };
    $.ajax({
        type : 'post',
        url : basePath + "job/saveJob.action",
        data : paramData,
        dataType : 'text',
        success : function(data) {
            if (data != null && data != "" && data == "success") {
                closeAndRefresh();
            } else {
                var content = '';
                if (type == "add") {
                    content='新增失败!';
                } else {
                    content='修改失败!';
                }
                layer.open({
                    title: '错误',
                    icon: 5,
                    content: content,
                    yes:function () {
                        closeAndRefresh();
                    }
                });
            }
            //提交按钮置为可用
            $("#submit").removeClass("layui-btn-disabled");
        }
    });
}
